package com.mijie.homi.service.user;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.mijie.homi.common.constant.MCKey;
import com.mijie.homi.common.domain.user.UserExtInfo;
import com.mijie.homi.dao.user.UserExtInfoDao;
import com.mijie.homi.service.cache.MemcacheService;
import com.mijie.homi.service.notify.NotifyUserAgent;

@Service
public class UserExtInfoService {

	@Autowired
	private UserExtInfoDao dao;
	@Autowired
	private UserService userService;
	@Autowired
	private NotifyUserAgent notifyUserAgent;
	@Autowired
	private MemcacheService mcs;
	
	private Logger logger  = Logger.getLogger(UserExtInfoService.class);
	
	public int settingAlbumCover(int userId,String albumCover){
		int rows = dao.settingAlbumCover(userId, albumCover);
		if(rows>0) mcs.delete(MCKey.USER_EXTINFO_BY_ID, String.valueOf(userId));
		return rows;
	}
	
	
	@Transactional(rollbackFor={Exception.class})
	public int settingMood(int id,long moodId,int mood){
		int rows =  dao.settingMood(id, moodId, mood);
		if(rows>0){
			mcs.delete(MCKey.USER_EXTINFO_BY_ID, String.valueOf(id));
			notifyUserAgent.sendNotifyToSubscribers(id);
		}
		return rows;
	}
	
	
	public int increIntegral(int id,int integral){
		int rows = dao.increIntegral(id, integral);
		if(rows>0) mcs.delete(MCKey.USER_EXTINFO_BY_ID, String.valueOf(id));
		return rows;
	}
	
	public int updateLocation(int id,double latitude,double longitude,String ip){
		int rows = dao.updateLocation(id, latitude, longitude,ip);
		if(rows>0){
			mcs.delete(MCKey.USER_EXTINFO_BY_ID, String.valueOf(id));
		}
		return rows;
	}
	
	@Transactional(rollbackFor={Exception.class})
	public int createUserExtInfo(UserExtInfo info){
		return dao.createUserExtInfo(info);
	}
	
	public UserExtInfo getUserExtInfoByUserId(int userId){
		UserExtInfo info = (UserExtInfo) mcs.get(MCKey.USER_EXTINFO_BY_ID, String.valueOf(userId));
		if(info==null){
			info =  dao.getUserExtInfoByUserId(userId);
			if(info!=null) mcs.set(MCKey.USER_EXTINFO_BY_ID, String.valueOf(userId), MCKey.USER_CACHE_TIMEOUT, info);
		}
		return info;
	}
	
	public List<UserExtInfo> listUserExtInfoByIds(List<Integer> list){
		return dao.listUserExtInfoByIds(list);
	}
	
	public int incrNumOfPhoto(int userId,int size){
		int rows = dao.incrNumOfPhoto(userId, size);
		if(rows>0) mcs.delete(MCKey.USER_EXTINFO_BY_ID, String.valueOf(userId));
		return rows;
	}
	
	@Transactional(rollbackFor={Exception.class})
	public int incrNumOfRelatedTopic(int userId){
		int rows = dao.incrNumOfRelatedTopic(userId);
		//retry
		if(rows==0) rows = dao.incrNumOfRelatedTopic(userId);
		if(rows>0) mcs.delete(MCKey.USER_EXTINFO_BY_ID, String.valueOf(userId));
		return rows;
	}
	
	@Transactional(rollbackFor={Exception.class})
	public int decrNumOfMood(int userId){
		int succ =  dao.decrNumOfMood(userId);
		if(succ>0) mcs.delete(MCKey.USER_EXTINFO_BY_ID, String.valueOf(userId));
		return succ;
	}
	
	@Transactional(rollbackFor={Exception.class})
	public int settingMoodAndDecrNumOfMood(int userId,int mood,long moodId){
		int succ = dao.settingMoodAndDecrNumOfMood(userId, mood, moodId);
		if(succ>0) mcs.delete(MCKey.USER_EXTINFO_BY_ID, String.valueOf(userId));
		return succ;
	}
	
	public int decrNumOfPhoto(int userId){
		int rows = dao.decrNumOfPhoto(userId);
		if(rows>0) mcs.delete(MCKey.USER_EXTINFO_BY_ID, String.valueOf(userId));
		return rows;
	}
	
	public int updateUserDeviceIdAndTerminalId(int userId,String deviceId,String terminalId){
		int rows = dao.updateUserDeviceIdAndTerminalId(userId, deviceId, terminalId);
		if(rows>0) mcs.delete(MCKey.USER_EXTINFO_BY_ID, String.valueOf(userId));
		return rows;
	}
	
}
